<?php


# log: for pop

define('DEBUG', true);

$debugFile = fopen("c:/tmp/log/" . date("Y-m-d") . ".log", 'a');

function _p($s) {
    global $debugFile;
    fwrite($debugFile, $s);
}

function _f() {
    global $debugFile;
    fflush($debugFile);
}

/**
 * print out the vars
 */
function l() {
    if (DEBUG) {
        _p(date("H:i:s") . " @ " . printTrace() . "\n\t\t");

        $flag = false;
        foreach (func_get_args () as $a) {
            if($flag){
                _p(" | ");
            } else {
                $flag = true;
            }
            if(is_array($a) || is_object($a)){
                _p(var_export($a, true));
            } else {
                _p($a);
            }
            $b = $a;
        }

        _p("\n");
        _f();
    }
}

/**
 * print out the vars
 */
function n() {
    if (DEBUG) {
        _p(date("H:i:s") . ":    ");

        $flag = false;
        foreach (func_get_args () as $a) {
            if($flag){
                _p(" | ");
            } else {
                $flag = true;
            }
            if(is_array($a) || is_object($a)){
                _p(var_export($a, true));
            } else {
                _p($a);
            }
            $b = $a;
        }

        _p("\n");
        _f();
    }
}
function fn(){}

/**
 * print out the vars : for tracing
 */
function t() {
    if (DEBUG) {
        _p(date("H:i:s") . " @ " . printTrace() . "\n\t\t");

        $flag = false;
        foreach (func_get_args () as $a) {
            if($flag){
                _p(" | ");
            } else {
                $flag = true;
            }
            if(is_array($a) || is_object($a)){
                _p(var_export($a, true));
            } else {
                _p($a);
            }
            $b = $a;
        }

        _p("\n");
        _f();
    }
}

function fl(){}
function ft(){}

function printTrace() {
    $t = debug_backtrace();
    foreach ($t as $f) {
        $funcName = $f['function'];
        if ($funcName != 'printTrace') {
            return "#" . $f['line'] . ", " . $f['file'];
        }
    }
}

/**
 * getting the debug backtrace as a string
 * @param <type> $traces_to_ignore the max number lines of the backtrace to be printed
 * @return <type>
 */
function get_debug_print_backtrace($traces_to_ignore = 1){
    $traces = debug_backtrace();
    $ret = array();
    foreach($traces as $i => $call){
        if ($i < $traces_to_ignore ) {
            continue;
        }

        $object = '';
        if (isset($call['class'])) {
            $object = $call['class'].$call['type'];
            if (is_array($call['args'])) {
                foreach ($call['args'] as &$arg) {
                    get_arg($arg);
                }
            }
        }

        $ret[] = '#'.str_pad($i - $traces_to_ignore, 3, ' ')
        .$object.$call['function'].'('.implode(', ', $call['args'])
        .') called at ['.$call['file'].':'.$call['line'].']';
    }

    return implode("\n",$ret);
}

function get_arg(&$arg) {
    if (is_object($arg)) {
        $arr = (array)$arg;
        $args = array();
        foreach($arr as $key => $value) {
            if (strpos($key, chr(0)) !== false) {
                $key = '';    // Private variable found
            }
            $args[] =  '['.$key.'] => '.get_arg($value);
        }

        $arg = get_class($arg) . ' Object ('.implode(',', $args).')';
    }
}

/**
 * This function is used for establishing helpers
 * @return <type> clean the buffer and return the buffer as a string
 */
function ob_end_contents(){
    $s = ob_get_contents();
    ob_end_clean();
    return $s;
}

$_first_run_testFuncs = true;
function testFuncs($funcName){
    global $_first_run_testFuncs;

    if($_first_run_testFuncs){
        $html = <<<script
   <script type='text/javascript'>
       $(".showHtml").live('click', function(){
            if($(this).next().css('display') == 'none'){
                $(this).next().css('display', 'block');
            } else {
                $(this).next().css('display', 'none');
            }
       });
       $(".showHtmlCode").live('click', function(){
            if($(this).next().css('display') == 'none'){
                $(this).next().css('display', 'block');
            } else {
                $(this).next().css('display', 'none');
            }
       });
   </script>
script;
        $_first_run_testFuncs = false;
        }

    $time1 = microtime(true);
    $res = $funcName();
    $time2 = microtime(true);
    $diff = $time2 - $time1;

    if(!is_array($res) || !isset($res['test']) || !isset($res['res'])){
        $html .= <<<html
   <div>
       Test for $funcName. Time spent is $diff.
   </div>
   <hr>
html;
    } else {
        if($res['test'] == 'ok'){
            $htmlRes = htmlentities($res['res']);
            $htmlText = $res['res'];
            $html .= <<<html
   <div> Test for $funcName. <span style='color:green'>Successful</span> Time spent is $diff. <br/>
       <div>$res[msg]</div>
       <input type='button' class='showHtml' value='Show Html' />
       <div style='display:none'>$htmlText</div>
       <input type='button' class='showHtmlCode' value='Show Html Code' />
       <div style='display:none'><pre>$htmlRes</pre></div>
   </div>
   <hr>
html;
        } else {
            $html .= <<<html
   <div> Test for $funcName. <span style='color:red'>Failed</span> Time spent is $diff. <br/> <div>$res[msg]</div></div> <hr>
html;
        }
    }
    return $html;
}


# pop's code: end here

?>